package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.MinioConfig;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.Department;
import com.jrtc.base.entity.bo.Patientfile;
import com.jrtc.base.util.MinioUtil;
import com.jrtc.base.util.UUIDUtil;
import com.jrtc.dao.DepartmentDao;
import com.jrtc.dao.PatientfileDao;
import com.jrtc.service.DepartmentService;
import com.jrtc.service.PatientfileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.util.Objects.isNull;

@Service("patientfileService")
public class PatientfileServiceImpl implements PatientfileService {
    /**
     * 空字符串
     */
    private static final String NULLSTR = "";

    @Resource
    private MinioUtil minioUtil;
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinioClient client;
    @Resource
    private PatientfileDao patientfileDao;
    /**
     * 患者文件上传
     * */
    @Override
    public List<String> patientFileUpload(List<MultipartFile> fileList, Integer id) throws Exception {
        if (fileList.size() > 0){
            List<String> uploadFileList = new ArrayList<>();
            for (MultipartFile multipartFile : fileList) {
                String uploadFile;
                try {
                    uploadFile = patientFileUpload(multipartFile, id);
                } catch (Exception e){
                    throw new Exception("文件上传失败");
                }

                uploadFileList.add(uploadFile);
            }
            return uploadFileList;
        }
        else {
            throw new Exception("没有接收到文件");
        }
    }

    /**
     * 查询患者文件列表
     * */
    @Override
    public List<Patientfile> selPatientFileUpload(Integer id) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Patientfile> patientfileList = patientfileDao.selPatientFileUpload(id);
        if (patientfileList != null && patientfileList.size() > 0){
            for (Patientfile patientfile : patientfileList) {
                if (patientfile.getCreateTime() != null){
                    patientfile.setCreateDate(format.format(patientfile.getCreateTime()));
                }
            }
        }
        return patientfileList;
    }

    /**
     * 删除文件
     * */
    @Override
    public int isDeletePatientFile(Integer id) {
        int i = patientfileDao.isDeletePatientFile(id);
        return i;
    }

    /**
     * 患者文件上传
     * */
    public String patientFileUpload(MultipartFile file, Integer id) throws Exception {
        String fileName = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        if (isNull(extension) || NULLSTR.equals(extension.trim())) {
            switch (file.getContentType()) {
                case "image/png":
                    extension = "png";
                case "image/jpg":
                    extension = "jpg";
                case "image/jpeg":
                    extension = "jpeg";
                case "image/bmp":
                    extension = "bmp";
                case "image/gif":
                    extension = "gif";
                default:
                    return "";
            }
        }

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

        String s = UUIDUtil.fastUUID().toString();
        fileName = dateFormat.format(now) + "/" + s + "." + extension;
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object("patient/file/" + fileName)
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        client.putObject(args);

        Patientfile patientfile = new Patientfile();
        patientfile.setOldFileName(file.getOriginalFilename());
        patientfile.setFileUrl("http://106.38.39.137:9000/" + minioConfig.getBucketName() + "/patient/file/" + fileName);
        patientfile.setFileSize(file.getSize()/1024 + "." + file.getSize()%1024 +"KB");
        patientfile.setCreateTime(now);
        patientfile.setNewFileName(s + "." + extension);
        patientfile.setPatientId(id);
        patientfile.setIsDelete("false");
        patientfileDao.insertPatientFile(patientfile);

        return "http://106.38.39.137:9000/" + minioConfig.getBucketName() + "/patient/file/" + fileName;
//        return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/patient/file/" + fileName;
//        return minioConfig.getBucketName() + "/patient/file/" + fileName;
    }
}